<?php
/** 
*
* @author Tom Martin (Poppertom69) 
*
* @package acp
* @version $Id: acp_post_bot.php 31M 2007-08-05 01:09:43Z (local) $
* @copyright (c) 2007 Poppertom69
* @license http://opensource.org/licenses/gpl-license.php GNU Public License 
*
*/

/**
* @ignore
*/
define('IN_PHPBB', true);
$phpbb_root_path = (defined('PHPBB_ROOT_PATH')) ? PHPBB_ROOT_PATH : './';
$phpEx = substr(strrchr(__FILE__, '.'), 1);

include($phpbb_root_path . 'common.' . $phpEx);
include($phpbb_root_path . 'includes/functions_display.' . $phpEx);
include($phpbb_root_path . 'includes/functions_posting.' . $phpEx);
include($phpbb_root_path . 'includes/message_parser.' . $phpEx);
global $config;

// Start session management
$user->session_begin();
$auth->acl($user->data);

		$user->setup('posting');
		$user->setup('anonymous');
//

if($auth->acl_get('u_post_bot')){

// Grab data
		$subject		= request_var('subject', '');
		$message		= request_var('message', '');
		$text			= utf8_normalize_nfc($message);
		$action			= request_var('Category', '');
		$sel			= request_var('SubCat', '');
		$submit			= (!empty($_POST['submit'])) ? true : false;
		
		$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'post_bot_id'";
			$result = $db->sql_query($sql);
			$poster_id =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);
			
		$sql = 'SELECT post_bot_value
				FROM ' . POST_BOT_TABLE . "
				WHERE post_bot_config = 'posts_need_approving'";
			$result = $db->sql_query($sql);
			$config_approve =  $db->sql_fetchfield('post_bot_value');
			$db->sql_freeresult($result);
		
				
		if ($submit)
	
	{
	

		if ($config_approve == 1){
		$post_approve = 0;
		}else{
		$post_approve = 1;
		}	


	//BBCode options
	$poll = $uid = $bitfield = $options = '';

	//Generate the text for storage in the database
	generate_text_for_storage($message, $uid, $bitfield, $options, true, true, true);
	
	if($action != create){
	
	$backup_user = array(
		'data'	=> $user->data,
		
		);
		
	$sql = 'SELECT username, user_colour
		FROM ' . USERS_TABLE . "
		WHERE user_id = '$poster_id'";
	$result	= $db->sql_query($sql);
	$row	= $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	
	

	$user->data = array_merge($user->data, array(
		'user_id'		=> (int) $poster_id,
		'username'		=> $row['username'],
		'user_colour'	=> $row['user_colour'],
		
	));	
			
	$sql = 'SELECT * FROM ' . TOPICS_TABLE . " WHERE topic_id = $sel";
	$result = $db->sql_query($sql);
	$post_forum = (int) $db->sql_fetchfield('forum_id');
	$db->sql_freeresult($result);
	
	$data = array( 
	'forum_id'			=> $post_forum,
	'topic_id'			=> $sel,
	'icon_id'			=> false,

	'enable_bbcode'		=> true,
	'enable_smilies'	=> true,
	'enable_urls'		=> true,
	'enable_sig'		=> true,

	'message'			=> $text,
	'message_md5'		=> md5($text),
				
	'bbcode_bitfield'	=> $bitfield,
	'bbcode_uid'		=> $uid,

	'post_edit_locked'	=> 0,
	'topic_title'		=> $subject,
	'notify_set'		=> false,
	'notify'			=> false,
	'post_time' 		=> 0,
	'forum_name'		=> '',
	'enable_indexing'	=> true,
	
	
	);

	submit_post('reply', $subject, '', POST_NORMAL, $poll, $data);	

	//Lets do the updating of the posts table
	$sql_ary = array(

							'post_approved' => $post_approve,
							'poster_ip'	=> '000.000.0.0',
							
							);							
														
		$sql = 'UPDATE ' . POSTS_TABLE . '
				SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
				WHERE post_subject =  '$subject'";
			$db->sql_query($sql);

	//Trigger error to say blog has been added
	$redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$post_forum&amp;t=$sel");
	meta_refresh(3, $redirect_url);

	trigger_error($user->lang['POST_STORED'] . '<br /><br />' . sprintf($user->lang['RETURN_FORUM'], '<a href="' . $redirect_url . '">', '</a>'));
	
	// Restore the $user->data array
	$user->data	= $backup_user['data'];
	unset($backup_user);
			

	}

	}

	if($action == create){
	
	$backup_user = array(
		'data'	=> $user->data,
		
		);
		
	$sql = 'SELECT username, user_colour
		FROM ' . USERS_TABLE . "
		WHERE user_id = '$poster_id'";
	$result	= $db->sql_query($sql);
	$row	= $db->sql_fetchrow($result);
	$db->sql_freeresult($result);
	
	

	$user->data = array_merge($user->data, array(
		'user_id'		=> (int) $poster_id,
		'username'		=> $row['username'],
		'user_colour'	=> $row['user_colour'],
		
	));	
			
	$data = array( 
	'forum_id'			=> $sel,
	'icon_id'			=> false,

	'enable_bbcode'		=> true,
	'enable_smilies'	=> true,
	'enable_urls'		=> true,
	'enable_sig'		=> true,

	'message'			=> $text,
	'message_md5'		=> md5($text),
				
	'bbcode_bitfield'	=> $bitfield,
	'bbcode_uid'		=> $uid,

	'post_edit_locked'	=> 0,
	'topic_title'		=> $subject,
	'notify_set'		=> false,
	'notify'			=> false,
	'post_time' 		=> 0,
	'forum_name'		=> '',
	'enable_indexing'	=> true,
	
	
	);

	submit_post('post', $subject, '', POST_NORMAL, $poll, $data);	

	//Lets do the updating of the posts table
	$sql_ary = array(

							'post_approved' => $post_approve,
							'poster_ip'	=> '000.000.0.0',
							
							);							
														
		$sql = 'UPDATE ' . POSTS_TABLE . '
				SET ' . $db->sql_build_array('UPDATE', $sql_ary) . "
				WHERE post_subject =  '$subject'";
			$db->sql_query($sql);
			
	
			
		//Trigger error to say blog has been added
	$redirect_url = append_sid("{$phpbb_root_path}viewtopic.$phpEx", "f=$sel&amp;t=$topic_id");
	meta_refresh(3, $redirect_url);

	trigger_error($user->lang['POST_STORED'] . '<br /><br />' . sprintf($user->lang['RETURN_FORUM'], '<a href="' . $redirect_url . '">', '</a>'));
	
	// Restore the $user->data array
	$user->data	= $backup_user['data'];
	unset($backup_user);

	}

//Form Output

	$sql = 'SELECT * FROM ' . TOPICS_TABLE . " WHERE topic_approved = 1";
	$result = $db->sql_query($sql);
	$rows = $db->sql_fetchrowset($result);
	
	foreach ($rows as $row)
		
	{						

	//Get the blogs user id
	$topic_id_table = $row['topic_id'];	
	$topic_id_title = $row['topic_title'];	
	$forum_id 		= $row['forum_id'];		

	//Check if current user has permission to post and see the other forums!
	if ($auth->acl_get('f_reply', $forum_id)){
	//Assign teh template variables for the blogs
	$template->assign_block_vars('topic', array(
		'S_TOPIC'	=> 'addOption(document.postform.SubCat,"' . $topic_id_table . '", "' . $topic_id_title . '");',
		
		));

	}	
	}

	$db->sql_freeresult($result);
	
	$sql = 'SELECT * FROM ' . FORUMS_TABLE . " WHERE parent_id > 0";
	$result = $db->sql_query($sql);
	$rows = $db->sql_fetchrowset($result);
	
	foreach ($rows as $row)
		
	{						

	//Get the blogs user id
	$forum_id_table = $row['forum_id'];	
	$forum_id_title = $row['forum_name'];
			
	//Check if current user has permission to post and see the other forums!
	if ($auth->acl_get('f_post', $forum_id_table)){
	//Assign teh template variables for the blogs
	$template->assign_block_vars('forum', array(
		'S_FORUM'	=> 'addOption(document.postform.SubCat,"' . $forum_id_table . '", "' . $forum_id_title . '");',
		
		));

		}
	}

	$db->sql_freeresult($result);
	
	


//Get the status of the following features
	$bbcode_status	= ($config['allow_bbcode'] && $config['auth_bbcode_pm'] && $auth->acl_get('u_pm_bbcode')) ? true : false;
	$smilies_status	= ($config['allow_smilies'] && $config['auth_smilies_pm'] && $auth->acl_get('u_pm_smilies')) ? true : false;
	$img_status		= ($config['auth_img_pm'] && $auth->acl_get('u_pm_img')) ? true : false;
	$flash_status	= ($config['auth_flash_pm'] && $auth->acl_get('u_pm_flash')) ? true : false;
	$url_status		= ($config['allow_post_links']) ? true : false;

	// Generate smiley listing
	generate_smilies('inline', 0);

	//Assign the features template variable
	$template->assign_vars(array(
		'BBCODE_STATUS'			=> ($bbcode_status) ? sprintf($user->lang['BBCODE_IS_ON'], '<a href="' . append_sid("{$phpbb_root_path}faq.$phpEx", 'mode=bbcode') . '">', '</a>') : sprintf($user->lang['BBCODE_IS_OFF'], '<a href="' . append_sid("{$phpbb_root_path}faq.$phpEx", 'mode=bbcode') . '">', '</a>'),
		'IMG_STATUS'			=> ($img_status) ? $user->lang['IMAGES_ARE_ON'] : $user->lang['IMAGES_ARE_OFF'],
		'FLASH_STATUS'			=> ($flash_status) ? $user->lang['FLASH_IS_ON'] : $user->lang['FLASH_IS_OFF'],
		'SMILIES_STATUS'		=> ($smilies_status) ? $user->lang['SMILIES_ARE_ON'] : $user->lang['SMILIES_ARE_OFF'],
		'URL_STATUS'			=> ($url_status) ? $user->lang['URL_IS_ON'] : $user->lang['URL_IS_OFF'],				
		'S_COMPOSE_PM'			=> true,
		'S_BBCODE_ALLOWED'		=> $bbcode_status,
		'S_SMILIES_ALLOWED'		=> $smilies_status,
		'S_BBCODE_IMG'			=> $img_status,
		'S_BBCODE_FLASH'		=> $flash_status,
		'S_BBCODE_QUOTE'		=> $quote_status,
		'S_BBCODE_URL'			=> $url_status,
	));

	// Build custom bbcodes array
	display_custom_bbcodes();

		}
		else
		{
		
		trigger_error($user->lang['NOT_AUTHORIZED'] );
		}
		
// Output the page


page_header('Anonymous Posting');



$template->set_filenames(array(
	'body' => 'anonymous.html'
));


page_footer();



?>
